﻿using BrowserXP.Models;
using System;
using System.Timers;
using System.Windows;
using System.Windows.Input;

namespace BrowserXP
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //托盘图标
        readonly System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
        //消息通知
        readonly GrowlNotifiactions growlNotifications = new GrowlNotifiactions();
        public MainWindow()
        {
            InitializeComponent();
            SetWindowLocation();
            //Nofify();
        }
        

        //设置窗口距离右上角100单位
        private void SetWindowLocation()
        {
            double screenHeight = SystemParameters.FullPrimaryScreenHeight;
            double screenWidth = SystemParameters.FullPrimaryScreenWidth;
            Left = screenWidth - 100;
            Top = 100;
            LocationChanged+= delegate
            {
                var offset = popup.HorizontalOffset;
                popup.HorizontalOffset = offset + 1;
                popup.HorizontalOffset = offset;
            };
        }
        

        //无标题栏窗口的移动
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //移动前先记录下位置
            double left = Left;
            double top = Top;
            DragMove();
            var element = (FrameworkElement)sender;
            if (e.ClickCount == 1)
            {
                var timer = new Timer(300);
                timer.AutoReset = false;
                timer.Elapsed += new ElapsedEventHandler((o, ex) => element.Dispatcher.Invoke(new Action(() =>
                {
                    var timer2 = (Timer)element.Tag;
                    timer2.Stop();
                    timer2.Dispose();
                    //如果位置没有发生改变则认为是弹出pop窗口
                    if (left == Left && top == Top)
                        Window_MouseClick(sender, e);
                })));
                timer.Start();
                element.Tag = timer;
            }
            if (e.ClickCount > 1)
            {
                var timer = element.Tag as Timer;
                if (timer != null)
                {
                    //如果pop没有关掉
                    if (popup.IsOpen)
                        popup.IsOpen = false;
                    timer.Stop();
                    timer.Dispose();
                    Window_MouseDoubleClick(sender, e);
                }
            }           
        }

        //单击，移动或者弹出pop
        private void Window_MouseClick(object sender, MouseButtonEventArgs e)
        {
            popup.IsOpen = !popup.IsOpen;
        }

        //双击打开dashboard窗口
        private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            BrowserWindow busWindow = new BrowserWindow();
            busWindow.Show();
        }

        #region 菜单
        //退出程序
        private void exit_Click(object sender, RoutedEventArgs e)
        {
            notifyIcon.Dispose();
            Application.Current.Shutdown();
        }

        //帮助
        private void help_Click(object sender, RoutedEventArgs e)
        {
            ShowMessage(sender, e);
        }
        //问题反馈
        private void support_Click(object sender, RoutedEventArgs e)
        {
            BrowserWindow busWindow = new BrowserWindow();
            busWindow.Show();
        }
        #endregion

        #region 托盘
        private void Nofify()
        {
            this.notifyIcon.BalloonTipText = "浏览器正在运行";
            this.notifyIcon.ShowBalloonTip(2000);
            this.notifyIcon.Text = "浏览器";
            this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            this.notifyIcon.Visible = true;
            this.notifyIcon.DoubleClick += notifyIcon_DoubleClick;
            System.Windows.Forms.MenuItem help = new System.Windows.Forms.MenuItem("帮助");
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
            //System.Windows.Forms.MenuItem baidu = new System.Windows.Forms.MenuItem("打开百度");
            help.Click += new EventHandler(Help);
            exit.Click += new EventHandler(Close);
            //baidu.Click += new EventHandler(OpenBaidu);
            //关联托盘控件
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { help,exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
        }

        void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            BrowserWindow window = new BrowserWindow();
            window.Show();
        }
        private void Help(object sender, EventArgs e)
        {
            ShowMessage(sender, e);
        }
        private void Close(object sender, EventArgs e)
        {
            notifyIcon.Dispose();
            System.Windows.Application.Current.Shutdown();
        }
        private void OpenBaidu(object sender, EventArgs e)
        {
            BrowserWindow window = new BrowserWindow("www.baidu.com");
            window.Show();
        }

        #endregion

        #region 测试
        private void ShowMessage(object sender, EventArgs e)
        {
            growlNotifications.AddNotification(new Notification { Title = "测试", ImageUrl = "Resources/Images/logo.jpg", Message = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." });
        }

        #endregion
        
    }
}
